package com.blog.web.controller.blog;

import com.blog.common.annotation.Log;
import com.blog.common.core.controller.BaseController;
import com.blog.common.core.domain.AjaxResult;
import com.blog.common.core.page.TableDataInfo;
import com.blog.common.enums.BusinessType;
import com.blog.common.utils.poi.ExcelUtil;
import com.blog.system.domain.blog.BlogComment;
import com.blog.system.domain.blog.vo.BlogArticleVO;
import com.blog.system.domain.blog.vo.BlogCommentVO;
import com.blog.system.service.blog.IBlogArticleService;
import com.blog.system.service.blog.IBlogCommentService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 博客评论Controller
 *
 * @author shibalover
 * @date 2024-04-02
 */
@RestController
@RequestMapping("/blog/comment")
public class BlogCommentController extends BaseController {
    @Autowired
    private IBlogCommentService blogCommentService;

    @Autowired
    private IBlogArticleService blogArticleService;

    /**
     * 查询博客评论列表
     */
    @PreAuthorize("@ss.hasPermi('blog:comment:list')")
    @GetMapping("/list")
    public TableDataInfo list(BlogCommentVO blogComment) {
        startPage();
        List<BlogCommentVO> list = blogCommentService.selectBlogCommentList(blogComment);
        return getDataTable(list);
    }

    /**
     * 导出博客评论列表
     */
    @PreAuthorize("@ss.hasPermi('blog:comment:export')")
    @Log(title = "博客评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BlogCommentVO blogComment) {
        List<BlogCommentVO> list = blogCommentService.selectBlogCommentList(blogComment);
        ExcelUtil<BlogCommentVO> util = new ExcelUtil<>(BlogCommentVO.class);
        util.exportExcel(response, list, "博客评论数据");
    }

    /**
     * 获取博客评论详细信息
     */
    @PreAuthorize("@ss.hasPermi('blog:comment:query')")
    @GetMapping(value = "/{commentId}")
    public AjaxResult getInfo(@PathVariable("commentId") Long commentId) {
        return success(blogCommentService.selectBlogCommentByCommentId(commentId));
    }

    /**
     * 新增博客评论
     */
    @PreAuthorize("@ss.hasPermi('blog:comment:add')")
    @Log(title = "博客评论", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BlogComment blogComment) {
        //检查属性
        if (ObjectUtils.isEmpty(blogComment.getArticleId())
                || ObjectUtils.isEmpty(blogComment.getComment())
                || ObjectUtils.isEmpty(blogComment.getUserId())) {
           return AjaxResult.warn("参数不能为空");
        }
        //修改评论数
        BlogArticleVO blogArticleVO = blogArticleService.selectBlogArticleByArticleId(blogComment.getArticleId());
        blogArticleVO.setCommentsCount(blogArticleVO.getCommentsCount() + 1);
        blogArticleService.updateBlogArticle(blogArticleVO);
        //插入评论
        return toAjax(blogCommentService.insertBlogComment(blogComment));
    }

    /**
     * 修改博客评论
     */
    @PreAuthorize("@ss.hasPermi('blog:comment:edit')")
    @Log(title = "博客评论", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BlogComment blogComment) {
        return toAjax(blogCommentService.updateBlogComment(blogComment));
    }

    /**
     * 删除博客评论
     */
    @PreAuthorize("@ss.hasPermi('blog:comment:remove')")
    @Log(title = "博客评论", businessType = BusinessType.DELETE)
    @DeleteMapping("/{commentIds}")
    public AjaxResult remove(@PathVariable Long[] commentIds) {
        return toAjax(blogCommentService.deleteBlogCommentByCommentIds(commentIds));
    }
}
